package tp.pr4;

import tp.pr4.items.Item;
import tp.pr4.items.ItemContainer;

/**
 * It represents a place in the city. Places are connected by streets 
 * according to the 4 compass directions, North, East, South and West. 
 * Every place has a name and a textual description about itself. This 
 * description is displayed when the robot arrives at the place.
 * A place can represent the spaceship where the robot is safe. When 
 * the robot arrives at this place, the application is over.
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class Place {
	private static final String LINE_SEPARATOR = System.lineSeparator();
	private String name = null;
	private boolean isSpaceShip = false;
	private String description = null;
	private ItemContainer it;
	
	/**
	 * Creates the place.
	 * @param name Place name
	 * @param isSpaceShip Is it a spaceship?
	 * @param description Place description
	 */
	public Place(String name,boolean isSpaceShip,String description){
		this.name = name;
		this.isSpaceShip = isSpaceShip;
		this.description = description;
		this.it = new ItemContainer();
	}
		
	/**
	 * Is it the spaceship?
	 * @return true if the place represents a spaceship.
	 */
	public boolean isSpaceship(){
		return this.isSpaceShip;
	}
	
	/**
	 * Overrides toString method. 
	 * @return the place name, its description and the list of items contained in the place
	 */
	public String toString(){
		String text = "";
		text += this.name + LINE_SEPARATOR + this.description + LINE_SEPARATOR;
		if(this.it.numberOfItems() > 0){
			text += "The place contains these objects: "+ this.it.toString();
		}
		else{
			text += "The place is empty. There are no objects to pick";
		}
		return text;
	}
	
	/**
	 * Tries to pick an item characterized by a given identifier from the place. 
	 * If the action was completed the item must be removed from the place.
	 * @param id The identifier of the item
	 * @return The item of identifier id if it exists in the place. Otherwise the method returns null
	 */
	public Item pickItem(String id){
		if(existItem(id)){
			return it.pickItem(id);
		}
		else return null;
	}
	
	/**
	 * Tries to add an item to the place. The operation can fail 
	 * (if the place already contains an item with the same name)
	 * @param it The item to be added
	 * @return true if and only if the item can be added to the place, i.e., 
	 * the place does not contain an item with the same name
	 */
	public boolean addItem(Item it){
		boolean Item = existItem(it.getId());
		if(Item == false){
			return this.it.addItem(it);
		}
		else return false;
	}
	
	
	/**
	 * Drop an item in this place. The operation can fail, returning false
	 * @param it The name of the item to be dropped.
	 * @return true if and only if the item is dropped in the place, i.e., an item 
	 * with the same identifier does not exists in the place
	 */
	public boolean dropItem(Item it){
		return addItem(it);
	}
	
	/**
	 * Checks if an item is in this place
	 * @param id Identifier of an item
	 * @return true if and only if the place contains the item identified by id
	 */
	public boolean existItem(String id){
		if (this.it.getItem(id) == null) return false; // Si el item es null quiere decir que no se ha encontrado
		else return true;
	}

}

